package com.example.normal.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.normal.service.IStudentService;
import com.example.utils.Consts;
import com.example.base.domain.PageVo;
import com.example.base.exception.CustomException;
import com.example.normal.entity.Student;
import com.example.normal.mapper.StudentMapper;
import com.example.utils.CrmUpdateGenerator;
import com.example.utils.CvtUtils;
import com.example.utils.StringUtils;
import kotlin.jvm.internal.Lambda;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.math.BigInteger;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author wahoyu
 * @since 2023-07-23
 */
@Service
@Slf4j
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements IStudentService {

    @Resource
    StudentMapper studentMapper;

    @Resource
    CrmUpdateGenerator crmUpdateGenerator;

    @Override
    public Page<Student> selectVoList(Student student, PageVo pageVo) {

        //设置逻辑删除标志
        student.setDeleteFlag(Consts.DELETE_FLAG.IN_USE);

        //设置分页参数
        if (pageVo.getPageNum() == null || pageVo.getPageSize() == null) {
            throw new CustomException("分页参数不能为空");
        }
        Page<Student> page = new Page<>(pageVo.getPageNum(), pageVo.getPageSize());
        LambdaQueryWrapper<Student> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Student::getDeleteFlag, Consts.DELETE_FLAG.IN_USE);

        // 判断属性是否为空，不为空则添加模糊查询条件
        if (StringUtils.isNotBlank(student.getName())) {
            queryWrapper.like(Student::getName, student.getName());
        }

        // 判断属性是否为空，不为空则添加查询条件
        if (StringUtils.isNotBlank(student.getPhone())) {
            queryWrapper.like(Student::getPhone, student.getPhone());
        }

        //根据创建时间倒序排序
        queryWrapper.orderByDesc(Student::getUpdateTime);

        return studentMapper.selectPage(page, queryWrapper);
    }

    @Override
    public List<JSONObject> getJsonObjList() {
        return studentMapper.getJsonObjList();
    }

    @Override
    public Map<String, Object> getOneMap(BigInteger id) {
        return studentMapper.getOneMap(id);
    }

    @Override
    public List<Map<String, Object>> getMapList() {
        return studentMapper.getMapList();
    }

    /**
     * 可置空更新
     * @author wanghongyu02
     * @date 2024-07-24 14:55
     * @param student
     * @return int
     */
    @Override
    public int updateWithNull(Student student) {

        //设置必要参数
        student.setUpdateTime(new Date());
        student.setUpdateBy("未知");

        //逻辑删除字段
        student.setDeleteFlag(Consts.DELETE_FLAG.IN_USE);

        //全量更新操作，使用自定义updateWrapper进行置空
        //其实把id放在需要忽略的字段里也没什么大用哈
        UpdateWrapper<Student> updateWrapper = crmUpdateGenerator.createUpdateWrapper(student, Student.class, StudentMapper.class,"id");
        updateWrapper.eq("id", student.getId()); //必须把id放在where里，不然就等着跑路吧
        studentMapper.update(student, updateWrapper);
        return 1;
    }

    /**
     * 添加学生信息
     * @author wanghongyu02
     * @date 2024-08-01 9:14
     * @param student
     * @return
     */
    @Override
    public void insert(Student student) {

        //设置必要参数
        student.setCreateTime(new Date());
        student.setUpdateTime(new Date());
        student.setCreateBy("未知");
        student.setUpdateBy("未知");
        //逻辑删除字段
        student.setDeleteFlag(Consts.DELETE_FLAG.IN_USE);

        studentMapper.insert(student);
    }

    /**
     * 根据id删除信息
     * @author wanghongyu02
     * @date 2024-08-01 9:14
     * @param id
     * @return java.lang.Integer
     */
    @Override
    public Integer deleteById(BigInteger id) {
        LambdaUpdateWrapper<Student> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(Student::getId, id);
        updateWrapper.set(Student::getDeleteFlag, Consts.DELETE_FLAG.DELETE);
        updateWrapper.set(Student::getDeleteTime, new Date());
        updateWrapper.set(Student::getDeleteBy, "未知");

        return studentMapper.update(null, updateWrapper);
    }

    /**
     * 分页查询学生信息（MybatisPlus+自定义Mapper）
     *
     * @param student
     * @param pageVo
     * @return Page<Student>
     * @author wanghongyu02
     * @date 2024-08-09 15:36
     */
    @Override
    public IPage<Student> testStudentPage(Student student, PageVo pageVo) {
        student.setDeleteFlag(Consts.DELETE_FLAG.IN_USE); //逻辑删除字段
        return studentMapper.testStudentPage(student, new Page<>(pageVo.getPageNum(), pageVo.getPageSize()));
    }

    @Override
    public List<Student> testWrapper() {

        Student student = new Student();
        student.setId(1L);

        LambdaQueryWrapper<Student> wrapper = new LambdaQueryWrapper<>(student);
        wrapper.eq(Student::getId, 1);

        return studentMapper.selectList(wrapper);
    }

    @Override
    public JSONObject testJSONObject(Student student) {
        JSONObject resultObj = studentMapper.testJSONObject(student);
        resultObj.put("childJson",studentMapper.testJSONObject(student));

        return CvtUtils.convertSnakeToCamelKeys(resultObj);
    }

}
